package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.xmy.cultivate.entity.RenewData;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xmy.cultivate.entity.ResidueStudent;
import com.xmy.cultivate.entity.views.ReNewDetailForTeacher;
import com.xmy.cultivate.entity.views.ResidueStatics;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author min
 * @since 2022-11-29
 */
public interface RenewDataMapper extends BaseMapper<RenewData> {

    //@Select("select  count(id) as count from renew_data where school_id = #{schoolId} and teacher_id =#{teacherId} and subjects_id = #{subjectsId} and year_part_renew=#{yearPart} and quarter_num_renew = #{quarterNum} and level=#{level} and year_class_id <>9 and year_class_id is not null and type=1")
    @Select("select  count(student_id) as count from renew_data ${ew.customSqlSegment}")
    public abstract Integer getShouldCount(@Param(Constants.WRAPPER) Wrapper wrapper);

    //@Select("select  count(id) as count from renew_data where school_id = #{schoolId} and teacher_id =#{teacherId} and subjects_id = #{subjectsId} and year_part_renew=#{yearPart} and quarter_num_renew = #{quarterNum} and level=#{level} and year_class_id <>9 and year_class_id is not null and type=1 and is_renew=#{isRenew}")
    @Select("select  count(student_id) as count from renew_data ${ew.customSqlSegment}")
    public abstract Integer getAlreadyShouldCount(@Param(Constants.WRAPPER) Wrapper wrapper);


    @Select("select * from renew_data ${ew.customSqlSegment}")
    public abstract IPage<ReNewDetailForTeacher> getStudentDetailList(IPage<?> page, @Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select student_id from renew_data ${ew.customSqlSegment}")
    public abstract List<Long> getDetailToStudentIds(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select t2.teacher_id AS teacher_id,t1.school_id As school_id, COUNT(DISTINCT t1.student_id) AS residueNumber from student_grade as t1 LEFT JOIN grade as t2 on t1.grade_id = t2.id ${ew.customSqlSegment}")
    public abstract List<ResidueStatics> residueStudentCount(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select t2.teacher_id AS teacher_id,t1.school_id As school_id, COUNT(DISTINCT t1.student_id) AS studentNumber from student_grade as t1 LEFT JOIN grade as t2 on t1.grade_id = t2.id ${ew.customSqlSegment}")
    public abstract List<ResidueStatics> residueStudentCountNext(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("SELECT sg.student_id as student_id, g.teacher_id as teacher_id,sg.school_id as school_id , COUNT(DISTINCT sg.student_id) AS studentNumber\n" +
            "FROM student_grade sg LEFT JOIN grade g\n" +
            "on sg.grade_id = g.id ${ew.customSqlSegment}")
    public abstract List<ResidueStatics> semesterStudentCount(@Param(Constants.WRAPPER) Wrapper wrapper);

    /**
     *
     * @param subjectsId
     * @param yearPart
     * @param quarterNum
     * @return
     */
    @Select("select student_id from order_detail where reading_status = 1 and deleted = 0 and `status`<>2 and residue_course_count>0 and residue_course_count<=9 and year_part = ${yearPart} and quarter_num =${quarterNum} and subjects_id = #{subjects_id}")
    public abstract List<String> residueBySubject(@Param("subjects_id") String subjectsId,@Param("yearPart") Integer yearPart,@Param("quarterNum") Integer quarterNum);

    /**
     *
     * @param subjectsId
     * @param yearPart
     * @param quarterNum
     * @return
     */
    @Select("select student_id from order_detail where reading_status = 1 and deleted = 0 and `status`<>2  and year_part = ${yearPart} and quarter_num =${quarterNum} and subjects_id = #{subjects_id}")
    public abstract List<String> studentAllBySubject(@Param("subjects_id") String subjectsId,@Param("yearPart") Integer yearPart,@Param("quarterNum") Integer quarterNum);

    /**
     *
     * @param yearPart
     * @param quarterNum
     * @return
     */
    @Select("select student_id from order_detail where reading_status = 1 and deleted = 0 and `status`<>2 and residue_course_count>0 and residue_course_count<=9 and year_part = ${yearPart} and quarter_num =${quarterNum}")
    public abstract List<String> residue(@Param("yearPart") Integer yearPart,@Param("quarterNum") Integer quarterNum);

    /**
     *
     * @param yearPart
     * @param quarterNum
     * @return
     */
    @Select("select student_id from order_detail where reading_status = 1 and deleted = 0 and `status`<>2  and year_part = ${yearPart} and quarter_num =${quarterNum}")
    public abstract List<String> studentAll(@Param("yearPart") Integer yearPart,@Param("quarterNum") Integer quarterNum);
    @Select("select * from order_detail ${ew.customSqlSegment}")
    public abstract List<ResidueStudent> residueStudent(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select year_class_id from grade where id = #{grade_id}")
    public abstract String getNianJi(@Param("grade_id") String gradeId);

    @Select("select  count(student_id) as count from order_detail ${ew.customSqlSegment}")
    public abstract List<Integer> residueStudentCounts(@Param(Constants.WRAPPER) Wrapper wrapper);
}

